package com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.base.response.RestResponse;
import com.css.fxfzxczh.modules.disasterSituationDataManage.samPoiHouSur.queryParams.SamPoiHouSurQuaParams;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.queryParams.QueryParams;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.service.LosRepEscalationService;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.service.impl.LosRepEscalationServiceImpl;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.vo.LosRepManageVo;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.queryParams.EarEveManageQuaParams;
import com.css.fxfzxczh.modules.synthesizeInfoManage.knowledgeManagement.vo.KnowledgeManagementVO;
import com.css.fxfzxczh.util.PlatformPageUtils;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @program
 * @description
 * @create 2022/8/8 10:39
 **/
@RestController
@RequestMapping("/losRepEsc")
@Slf4j
public class LosRepEscalationController {
    @Resource
    LosRepEscalationService losRepEscalationService;

    /**
     * 分页列表
     *
     * @param queParams
     * @return
     */
    @GetMapping("/getPage")
    public RestResponse getPage(QueryParams queParams, HttpServletRequest request) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = losRepEscalationService.getPage(queParams, curPage, pageSize);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 审核（批量审核）
     *
     * @param params
     * @return
     */
    @PostMapping("/check")
    public RestResponse check(@RequestBody QueryParams params) {
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try {
            List<String> idList = Arrays.asList(params.getIds().split(","));
            losRepEscalationService.check(idList, params.getCheckStatus(), params.getCheckOpinion());
            jsonObject.put("message","审核成功");
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "审核失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 导入
     * @param file
     * @return
     */
    @PostMapping("/reportImport")
    public RestResponse reportImport(@RequestParam("file") MultipartFile file,
                                     @RequestParam("category") String category,
                                     @RequestParam("attachmentId") String attachmentId,
                                     @RequestParam("xsemId") String xsemId,
                                     @RequestParam("isHavaLossValue") String isHavaLossValue){
        RestResponse response = null;
        try {
            String str=losRepEscalationService.reportImport(file,category,xsemId,isHavaLossValue,attachmentId);
            response = RestResponse.succeed(str);
        } catch (Exception e) {
            String errormessage = "导入失败,请检查模板内容!";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /***
     * 提交
     * @param
     * @return
     */
    @PostMapping("/submit/{ids}")
    public RestResponse submit(@PathVariable("ids") String ids){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            losRepEscalationService.submit(ids);
            jsonObject.put("message","提交成功");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "提交失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("findById")
    public RestResponse findById(String id) {
        try {
            LosRepManageVo data = losRepEscalationService.findById(id);
            return RestResponse.succeed(data);
        } catch (Exception e) {
            log.error("失败!", e);
            return RestResponse.fail("失败!");
        }
    }

}
